﻿// ******************************************************************************
// Developed by Oguz Demirel
// Date: March 17, 2010
// ******************************************************************************
namespace BulkListActions
{
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;
    using Microsoft.SharePoint.WebControls;
    using Microsoft.SharePoint.WebPartPages;
    using System;
    using System.Globalization;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;
    using System.Collections.Specialized;
    using System.Net;

    public abstract class CustomActionBase : MenuItemTemplate
    {
        #region ..:: Properties ::..

        private bool searchedForListView = false;
        public string handlerFileName = string.Empty;
        public string actionName = string.Empty;
        protected bool askForConfirmation = true;

        private ListViewWebPart listViewWebPart;
        private ListViewWebPart ListViewWebPart
        {
            get
            {
                if (!searchedForListView)
                {
                    listViewWebPart = Helper.FindListView(this.Parent);
                }
                return listViewWebPart;
            }
        }


        #endregion

        #region ..:: Constructor ::..

        public CustomActionBase()
        {
            // Assign UI properties here
            // text, image and description
            SetUIDetails();
        }

        #endregion ..:: Constructor ::..

        #region ..:: Rendering Methods ::..

        [AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        protected override void OnPreRender(System.EventArgs args)
        {
            base.OnPreRender(args);
            if (this.ListViewWebPart == null)
                return;

            Helper.RegisterScript(Page, this.ClientID, this.ListViewWebPart);
        }

        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true), AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
        protected override void Render(HtmlTextWriter output)
        {
            ListViewWebPart part = Helper.FindListView(this.Parent);
            if (part == null)
            {
                this.Visible = false;
            }
            if (this.Visible)
            {
                // If custom action requires user to confirm before taking any action
                // Register clintside script for confirmation as well
                if (askForConfirmation)
                {
                    string str = string.Format(CultureInfo.InvariantCulture, "{0}/_layouts/Zieglers/BulkListActions/" + handlerFileName + "?List={1}&View={2}&Item=", new object[] { SPContext.Current.Web.Url, part.ListName, part.ViewGuid });
                    string str2 = string.Format(CultureInfo.InvariantCulture, "WebPart{0}", new object[] { part.Qualifier });
                    string str3 = string.Format(CultureInfo.InvariantCulture, "window.location = '{0}' + GetSelectedItemsString('{1}') + '&referrer=' + escape(window.location)", new object[] { str, str2 });
                    base.ClientOnClickScript = string.Format(CultureInfo.InvariantCulture, "if(ListItemSelection_ConfirmAction('{0}', '{2}')){{{1}}};", new object[] { str2, str3, actionName });
                }
                else
                {
                    string clientScript = string.Format(CultureInfo.InvariantCulture, "ListItemSelection_ButtonClick('{0}', 'WebPart{1}')", this.ClientID, listViewWebPart.Qualifier);
                    this.ClientOnClickScript = clientScript;
                }
            }
            base.Render(output);
        }

        #endregion ..:: Rendering Methods ::..

        #region ..:: Abstract Methods ::..
        
        // ****************************************************
        // NOTE: This method MUST be implemented by deriving classes
        // since it's declared as abstract
        // ----------------------------------------------------
        // This method assigns UI related properties
        // for this custom action. 
        // Provide text, image, description, action name
        // for your action here
        public abstract void SetUIDetails();

        #endregion ..:: Abstract Methods ::..

    }

}
